package cn.lg.soar.system.general.service.impl;

import cn.lg.soar.common.util.AssertUtil;
import cn.lg.soar.database.base.service.impl.BaseServiceImpl;
import cn.lg.soar.system.config.AuthCacheKey;
import cn.lg.soar.system.general.entity.Dict;
import cn.lg.soar.system.general.mapper.DictMapper;
import cn.lg.soar.system.general.service.IDictItemService;
import cn.lg.soar.system.general.service.IDictService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.List;

/**
 * @author luguoxiang
 * @date 2021/8/15
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
public class DictServiceImpl extends BaseServiceImpl<DictMapper, Dict> implements IDictService {

    @Autowired
    private IDictItemService dictItemService;

    @Cacheable(value = AuthCacheKey.DICT_TYPE, key = "'codes'", cacheManager = "local")
    @Override
    public List<String> codeList() {
        return listObjs(
                Wrappers.<Dict>lambdaQuery()
                        .select(Dict::getCode),
                Object::toString
        );
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void remove(Collection<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        dictItemService.removeByDictIds(ids);
        AssertUtil.isTrue(super.removeByIds(ids), "删除失败");
    }
}
